package cn.manyufun.myf.ui.comics.model;

import org.json.JSONException;
import org.json.JSONObject;

import cn.manyufun.myf.api.Api;
import cn.manyufun.myf.api.HostType;
import cn.manyufun.myf.bean.BaseBean;
import cn.manyufun.myf.bean.ComicsDetailBean;
import cn.manyufun.myf.common.baserx.RxSchedulers;
import cn.manyufun.myf.common.commonutils.APIUtils;
import cn.manyufun.myf.common.commonutils.DisplayUtil;
import cn.manyufun.myf.ui.comics.contract.ComicsDetailContract;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observable;

/**
 * Created by Xiao on 2018-02-06.
 */

public class ComicsDetailModel implements ComicsDetailContract.Model {
    @Override
    public Observable<ComicsDetailBean> getComicsDetailData(long comicsId) {
        JSONObject root = new JSONObject();
        JSONObject head = APIUtils.getHeadParamter();
        try {
            root.put("Head", head);
            root.put("ImageSize", "0*" + DisplayUtil.dip2px(250f));
            root.put("ComicId", comicsId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        return Api.getDefault(HostType.manyufun).getComicsDetail(requestBody)
                .compose(RxSchedulers.<ComicsDetailBean>io_main());
    }

    @Override
    public Observable<BaseBean> followComics(int actionType, long comicsId) {
        JSONObject root = new JSONObject();
        JSONObject head = APIUtils.getHeadParamter();
        try {
            root.put("Head", head);
            root.put("ActionType", actionType);
            root.put("ComicId", comicsId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        return Api.getDefault(HostType.manyufun).followComics(requestBody)
                .compose(RxSchedulers.<BaseBean>io_main());
    }
}
